package com.wanjia.estate.service.bdservice.zydjspireservice;

import com.wanjia.estate.constant.FwxzType;
import com.wanjia.estate.constant.GyfsType;
import com.wanjia.estate.constant.ZdzhType;
import com.wanjia.estate.constant.tex.QlxzType;
import com.wanjia.estate.pojo.entity.Identity;
import com.wanjia.estate.properties.FileTemplateProperties;
import com.wanjia.estate.utils.MD5Util;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

@Service
public class ExcelReaderWriterZydjService {

    @Autowired
    private FileTemplateProperties fileTemplateProperties;

    public String ExcelReaderWriter(Identity identity,String folder) {
        String md5FileName = MD5Util.getMD5(identity.getSerialNumber());
        try {
            FileInputStream inputStream = new FileInputStream(fileTemplateProperties.getZjbdczydjsqspath());
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            //编号
            Row row2 = sheet.getRow(1);
            row2.getCell(0).setCellValue(identity.getSerialNumber());

            //登记申请人情况-转让方
            Row row3 = sheet.getRow(2);
            row3.getCell(3).setCellValue(identity.getZrfxm());
            row3.getCell(5).setCellValue(identity.getZrfzjh());
            Row row4 = sheet.getRow(3);
            row4.getCell(3).setCellValue(GyfsType.get(identity.getZrfgyfs()).getName());//identity.getZrfgyfs()
            row4.getCell(5).setCellValue(identity.getZrflxdh());
            Row row5 = sheet.getRow(4);
            row5.getCell(3).setCellValue(identity.getZrfdlrxm());
            row5.getCell(5).setCellValue(identity.getZrfdlrzjh());

            //登记申请人情况-受让方
            Row row6 = sheet.getRow(5);
            row6.getCell(3).setCellValue(identity.getSrfxm());
            row6.getCell(5).setCellValue(identity.getSrfzjh());
            Row row7 = sheet.getRow(6);
            row7.getCell(3).setCellValue(identity.getSrfgyfs());
            row7.getCell(5).setCellValue(identity.getSrflxdh());
            Row row8 = sheet.getRow(7);
            row8.getCell(3).setCellValue(identity.getSrfdlrxm());
            row8.getCell(5).setCellValue(identity.getSrfdlrzjh());

            //不动产情况
            Row row9 = sheet.getRow(8);
            row9.getCell(3).setCellValue(identity.getYbdcqzs());
            Row row10 = sheet.getRow(9);
            row10.getCell(3).setCellValue(identity.getZl());
            Row row11 = sheet.getRow(10);
            row11.getCell(3).setCellValue(identity.getBdcdyh());
            Row row12 = sheet.getRow(11);
            row12.getCell(3).setCellValue(identity.getBdclx());
            row12.getCell(5).setCellValue(FwxzType.get(identity.getFwxz()).getName());//identity.getFwxz()
            Row row13 = sheet.getRow(12);
            row13.getCell(3).setCellValue(identity.getMj());
            row13.getCell(5).setCellValue(ZdzhType.get(identity.getZd()).getName());//identity.getZd()
            Row row14 = sheet.getRow(13);
            row14.getCell(3).setCellValue(QlxzType.get(identity.getZdqlxz()).getName());
            row14.getCell(5).setCellValue(identity.getYhlx());
            Row row15 = sheet.getRow(14);
            row15.getCell(3).setCellValue(identity.getGzwlx());
            row15.getCell(5).setCellValue(identity.getLz());

            //询问笔录转让方
            Row row19 = sheet.getRow(18);
            row19.getCell(5).setCellValue(identity.getZrfwta());
            Row row20 = sheet.getRow(19);
            row20.getCell(5).setCellValue(identity.getZrfwtb());
            Row row21 = sheet.getRow(20);
            row21.getCell(5).setCellValue(identity.getZrfwtc());

            //询问笔录受让方
            Row row22 = sheet.getRow(21);
            row22.getCell(5).setCellValue(identity.getSrfwta());
            Row row23 = sheet.getRow(22);
            row23.getCell(5).setCellValue(identity.getSrfwtb());
            Row row24 = sheet.getRow(23);
            row24.getCell(5).setCellValue(identity.getSrfwtc());
            Row row25 = sheet.getRow(24);
            row25.getCell(5).setCellValue(identity.getSrfwtd());
            Row row26 = sheet.getRow(25);
            row26.getCell(5).setCellValue(identity.getSrfwte());
            Row row27 = sheet.getRow(26);
            row27.getCell(5).setCellValue(identity.getSrfwtf());
            Row row28 = sheet.getRow(27);
            row28.getCell(5).setCellValue(identity.getSrfwtg());

            //委托事项
            Row row29 = sheet.getRow(28);
            String wtsx ="";
            for (String s : identity.getWtsx()) {
                wtsx += s;
            }
            row29.getCell(1).setCellValue(wtsx);

            FileOutputStream outputStream = new FileOutputStream(folder+"/"+md5FileName+".xlsx");
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        }
        File file = new File(folder+"/"+md5FileName+".xlsx");
        return file.getPath();
    }
    }

